<?php
namespace Core\Helper;

class CommonHelper{

    public static  function ckEditorPreparer($vTexte)
    {
        $aTexte = explode("\n",$vTexte);
        for ($i=0;$i<count($aTexte)-1;$i++)
        {$aTexte[$i] .= '\\';}
        return implode("\n",$aTexte);
    }

    public static function getBaseUrl($url){
        $tmpArr = explode('/:', $url);

        return Zend_Controller_Front::getInstance()->getBaseUrl().'/'.$tmpArr[0];
    }

    public static function getBaseUrlForMenuController($url){
        if(strlen($url) > 2 && strpos($url,"/", strlen($url) - 2))
            $tmp = substr($url, 0, -1);
        else
            $tmp = $url;
//        $tmpArr = explode('/:', $url);
        return Zend_Controller_Front::getInstance()->getBaseUrl().'/'.$tmp;
    }

    public static function extractBaseUrl($fullRoute)
    {
        $baseLength = count(Zend_Controller_Front::getInstance()->getBaseUrl() . '/');
        return substr($fullRoute, $baseLength-1);

    }
    public static function rand_string( $length ) {
        $str ='';
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        $size = strlen( $chars );
        for( $i = 0; $i < $length; $i++ ) {

            $str .= $chars[ rand( 0, $size - 1 ) ];
        }

        return $str;
    }

    public static function vietnameseToASCII($sample){
        $marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
            "ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
        ,"ế","ệ","ể","ễ",
            "ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
        ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ",
            "đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
        ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
        ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ", " ", "\\" ,"/", "!","@","#","$","%","^","&","*","(",")","_","+","~","`",":",";","'",'"',"<",">",",",".","?","|");
        $marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
        ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
        ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
        ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
        ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D", "-", "-" ,"-", "-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-","-");
        return str_replace($marTViet,$marKoDau,$sample);
    }


    public static $DEFAULT_BLANK_IMAGE = '/core/photo_required.jpg';
    public static  function getImagePath( $imagePath){
        if(!$imagePath)
            return STATIC_IMAGE_WEB_ROOT.'/'.self::$DEFAULT_BLANK_IMAGE;
        return $imagePath;
    }

    public static function isFront(){
        $front = Zend_Controller_Front::getInstance();
        $module = $front->getRequest()->getParam("module");
        if($module == Pimcore_Config::getFrontModule()) return true;
        return false;
    }
}

